package phoenix.web.test_objects;

import phoenix.core.PhoenixManager;
import phoenix.core.base.extensions.TestBaseWebExt;
import phoenix.web.test_objects.data_providers.RegistrationDataProviders;

/**
 * Class contains test methods for Index page
 */
public class RegistrationTests extends TestBaseWebExt
{
	public RegistrationTests(PhoenixManager p_phoenix)
	{
		super(p_phoenix);
	}

	public RegistrationDataProviders dataProviders = new RegistrationDataProviders(manager);

	/**
	 * Registration test
	 * @param p_gender
	 * @param p_day
	 * @param p_month
	 * @param p_year
	 * @param p_email
	 * @param p_password
	 */
	public void registrationTest(String p_gender, String p_email, String p_password, String p_day, String p_month, String p_year)
	{
		try
		{
			page().closeSession();

			if (manager.isNF())
			{
				page().indexPage().closeIndexPopup();
			}

			testUser().setPostcode(page().indexPage().getPostcode());

			if (manager.isCNF() || manager.isWB() || manager.isFlirt() || manager.isFGF() || manager.isBN() || manager.isNF() || manager.isMD())
			{
				page().indexPage().fillMainForm(p_gender, p_email, p_password, p_day, p_month, p_year);
			}
			else if (manager.isLGW())
			{
				page().indexPage().fillThreeStepsForm(p_gender, p_email, p_password, p_day, p_month, p_year);
			}

			page().indexPage().submitRegistrationForm();
			page().welcomePage().waitForPageLoad();
			testUser().setRegDate(cronos().getCurrentDate("yyyy-MM-dd HH:mm:ss"));

		}
		catch (Exception p_ex)
		{
			log().errorAndScreen("registrationTest: " + p_ex.toString());
		}

		test().forErrors();
	}

	/**
	 * Date of birth field validation test
	 * @param p_day
	 * @param p_month
	 * @param p_year
	 * @param p_expected
	 */
	public void birthValidationTest(String p_day, String p_month, String p_year, boolean p_expected)
	{
		try
		{
			if (manager.isNF())
			{
				page().indexPage().closeIndexPopup();
			}

			page().indexPage().setDateOfBirth(p_day, p_month, p_year);
			page().indexPage().clickSubmit();
			boolean p_actual = page().indexPage().isDateValid(p_expected);
			test().assertEquals(p_actual, p_expected, "Date of Birth select validation: " + p_day + "-" + p_month + "-" + p_year);
		}
		catch (Exception p_ex)
		{
			log().errorAndScreen("birthValidationTest: " + p_ex.toString());
		}

		test().forErrors();
	}

	/**
	 * Email field validation test
	 * @param p_email
	 * @param p_expected
	 */
	public void emailValidationTest(String p_email, boolean p_expected)
	{
		try
		{
			page().indexPage().setEmail(p_email);
			page().indexPage().clickSubmit();
			boolean p_actual = page().indexPage().isEmailValid(p_expected);
			test().assertEquals(p_actual, p_expected, "Email field validation: " + p_email);
		}
		catch (Exception p_ex)
		{
			log().errorAndScreen("emailValidationTest: " + p_ex.toString());
		}

		test().forErrors();
	}

	/**
	 * Password field validation test
	 * @param p_password
	 * @param p_expected
	 */
	public void passwordValidationTest(String p_password, boolean p_expected)
	{
		try
		{
			page().indexPage().setPassword(p_password);
			page().indexPage().clickSubmit();
			boolean p_actual = page().indexPage().isPasswordValid(p_expected);
			test().assertEquals(p_actual, p_expected, "Password field validation: " + p_password);
		}
		catch (Exception p_ex)
		{
			log().errorAndScreen("passwordValidationTest: " + p_ex.toString());
		}

		test().forErrors();
	}

	/**
	 * Location field validation test
	 * @param p_location
	 * @param p_expected
	 */
	public void locationValidationTest(String p_location, boolean p_expected)
	{
		try
		{
			page().indexPage().setLocation(p_location);
			page().indexPage().clickSubmit();
			boolean p_actual = page().indexPage().isLocationValid(p_expected);
			test().assertEquals(p_actual, p_expected, "Location field validation: " + p_location);
		}
		catch (Exception p_ex)
		{
			log().errorAndScreen("locationValidationTest: " + p_ex.toString());
		}

		test().forErrors();
	}

	/**
	 * Terms link test
	 */
	public void termsTest()
	{
		try
		{
			String p_link = page().indexPage().getLinkTerms();
			boolean p_actual = manager.http().checkLinkForLiving(p_link);
			test().assertEquals(p_actual, true, "Terms link: " + p_link);
		}
		catch (Exception p_ex)
		{
			log().errorAndScreen("termsTest: " + p_ex.toString());
		}

		test().forErrors();
	}

	/**
	 * Privacy link test
	 */
	public void privacyTest()
	{
		try
		{
			String p_link = page().indexPage().getLinkPrivacy();
			boolean p_actual = manager.http().checkLinkForLiving(p_link);
			test().assertEquals(p_actual, true, "Privacy link: " + p_link);
		}
		catch (Exception p_ex)
		{
			log().errorAndScreen("privacyTest: " + p_ex.toString());
		}

		test().forErrors();
	}

	/**
	 * Email field re send email test
	 * @param p_email
	 * @param p_expected
	 */
	public void resendEmailTest(String p_email, String p_expected)
	{
		try
		{
			page().indexPage().setEmail(p_email);
			page().indexPage().clickSubmit();
			String p_actual = page().indexPage().getResendEmailText(p_email);
			test().assertEquals(p_actual, p_expected, "Resend email text: " + p_actual);
		}
		catch (Exception p_ex)
		{
			log().errorAndScreen("resendEmailTest: " + p_ex.toString());
		}

		test().forErrors();
	}
}
